package org.example.controller;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.example.pojo.*;
import org.example.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 商品列表
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param goodsName
     * @return
     */
    @GetMapping("/list")
    public Result<PageBean> list( Integer pageNum,Integer pageSize,Integer categoryId, String goodsName,Integer vendorId){
        log.info("page:{},pageSize:{},categoryId:{},goodsName:{}",pageNum,pageSize,categoryId,goodsName);
        PageBean<Goods> pageBean = goodsService.list(pageNum, pageSize, categoryId, goodsName, vendorId);
        return Result.success(pageBean);
    }

    /**
     * 库存小于10的列表
     */
    @GetMapping("/stock")
    public Result<PageBean> list2( Integer pageNum,Integer pageSize,Integer categoryId, String goodsName,Integer vendorId){
        PageBean<Goods> pageBean = goodsService.list2(pageNum, pageSize, categoryId, goodsName, vendorId);
        return Result.success(pageBean);
    }

    /**
     * 分页临期商品列表
     */
    @GetMapping("/expire")
    public Result expireList(Integer pageNum,Integer pageSize,String goodsName,Integer category ){
        PageBean<ExpireGoodsVo> pageBean = goodsService.expireList(pageNum, pageSize, goodsName, category);
        return Result.success(pageBean);
    }

    /**
     * 库存临期商品列表
     */
    @GetMapping("/overdue")
    public Result<List<GoodsOverdueVO>> list3(){
        List<GoodsOverdueVO> list = goodsService.list3();
        return Result.success(list);
    }

    /**
     * 添加商品
     * @param goods
     * @return
     */
    @PostMapping("/add")
    public Result<Void> add(@RequestBody Goods goods){
        if (goods.getProduceDate()==null||goods.getOverdueDate()==null){
            return Result.error("生产日期或临期日期不能为空");
        }

        goodsService.add(goods);
        return Result.success();
    }

    /**
     * 修改商品
     * @param goods
     * @return
     */
    @PutMapping("/update")
    public Result<Void> update(@RequestBody Goods goods){
        log.info("修改商品:{}",goods);
        goodsService.update(goods);
        return Result.success();
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除商品:{}",id);
        goodsService.delete(id);
        return Result.success();
    }

    /**
     * 批量删除商品
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Integer> ids){
        log.info("批量删除商品:{}",ids);
        goodsService.batchDelete(ids);
        return Result.success();
    }

    /**
     * 查询商品列表
     */
    @GetMapping("/all")
    public Result list(){
        List<Goods> allGoods = goodsService.getAllGoods();
        List<Map<String,Object>> list = new ArrayList<>();
        allGoods.forEach(goods -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", goods.getId());
            map.put("name", goods.getGoodsName());
            list.add(map);
        });
        return Result.success(list);
    }
}
